package cn.android.weblog.api;

import com.alibaba.fastjson.JSON;

import net.minidev.json.JSONObject;

import org.jsoup.Connection;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.android.weblog.domain.Catagory;
import cn.android.weblog.domain.Passage;
import cn.android.weblog.util.HttpUtils;

public class PassageAPI {

    /**
     * 添加文章
     * @param passage
     * @param userId
     * @return
     */
    public String add(Passage passage, Integer userId){
        Map<String,String> params=new HashMap<>();
        params.put("catagoryId",passage.getCatagoryId().toString());
        params.put("title",passage.getTitle());
        params.put("described",passage.getDescribed());
        params.put("content",passage.getContent());
        params.put("originalPassageId",passage.getOriginalPassageId());  //转载文章id
        params.put("isDraft",passage.getIsDraft().toString());
        params.put("isOriginal",passage.getIsOriginal().toString());
        params.put("isPrivate",passage.getIsPrivate().toString());
        Connection.Response response = null;
        try {
            response= HttpUtils.post("/passage/add/"+userId, JSONObject.toJSONString(params));
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(response.body());
        return response.body();
    }


    /**
     * 查询回收站
     * @param userId
     * @return
     */
    public List<Passage> findDeleted(Integer userId){
        Connection.Response response=null;
        try {
            response=HttpUtils.get("/passage/findDelete/"+userId);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Map<String,Object> map =(Map<String,Object>)JSON.parse(response.body());
        return JSON.parseArray(map.get("passageList").toString(), Passage.class);
    }

    /**
     * 查询草稿箱
     * @param userId
     * @return
     */
    public List<Passage> findDraft(Integer userId){
        Connection.Response response=null;
        try {
            response=HttpUtils.get("/passage/findDraft/"+userId);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Map<String,Object> map =(Map<String,Object>)JSON.parse(response.body());
        return JSON.parseArray(map.get("passageList").toString(), Passage.class);
    }

    /**
     * 查询用户发布的文章列表
     * @param userId
     * @return
     */
    public List<Passage> findPassageByUserId(Integer userId){
        Connection.Response response=null;
        try {
            response=HttpUtils.get("/passage/findByUserId/"+userId);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Map<String,Object> map =(Map<String,Object>)JSON.parse(response.body());
        return JSON.parseArray(map.get("passageList").toString(), Passage.class);
    }

    /**
     * 查询用户收藏的文章列表
     * @param userId
     * @return
     */
    public List<Passage> findCollectByUserId(Integer userId){
        Connection.Response response=null;
        try {
            response=HttpUtils.get("/passage/findCollectPassage/"+userId);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Map<String,Object> map =(Map<String,Object>)JSON.parse(response.body());
        return JSON.parseArray(map.get("passageList").toString(), Passage.class);
    }

    /**
     * 根据文章id查询详情信息
     * @param passageId
     * @return
     */
    public Map<String,Object> findPassageById(Integer passageId){
        Connection.Response response=null;
        try {
            response=HttpUtils.get("/passage/findPassage/"+passageId);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Map<String,Object> results =(Map<String,Object>)JSON.parse(response.body());
        return results;
    }

    /**
     * 首页初始化信息
     * @return
     */
    public Map<String,Object> initIndex(){
        Connection.Response response=null;
        try {
            response=HttpUtils.get("/passage/findPassages");
        } catch (IOException e) {
            e.printStackTrace();
        }
        Map<String,Object> results =(Map<String,Object>)JSON.parse(response.body());
        return results;
    }

    /**
     * 根据关键词ID查询文章
     * @param keywordId
     * @return
     */
    public Map<String,Object> listByKeyword(Integer keywordId){
        Connection.Response response = null;
        try {
            response = HttpUtils.get("/keyword/findPassage/" + keywordId);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Map<String,Object> results =(Map<String,Object>)JSON.parse(response.body());
        return results;
    }

}
